package com.zxl.yjb.activity.payfee;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zxl.yjb.R;
import com.zxl.yjb.base.BaseActivity;
import com.zxl.yjb.bean.MessageEvent;
import com.zxl.yjb.utils.PayFeeUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShareMessagePayFeeActivity extends BaseActivity {


    @BindView(R.id.iv_zfb_pay)
    ImageView ivZfbPay;
    @BindView(R.id.ll_zfb_pay)
    LinearLayout llZfbPay;
    @BindView(R.id.iv_wx_pay)
    ImageView ivWxPay;
    @BindView(R.id.ll_wx_pay)
    LinearLayout llWxPay;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.tv_pay_fee)
    TextView tvPayFee;

    private int payMethod = 1; // 1 支付宝 ；2 微信
    private int order_id;
    private String order_sn = "";
    private String money = "";
    private String money_cal = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sharemessage_payfee);
        ButterKnife.bind(this);
        order_id = getIntent().getIntExtra("order_id",0);
        order_sn = getIntent().getStringExtra("order_sn");
        money = getIntent().getStringExtra("money");
        money_cal = getIntent().getStringExtra("money_cal");

        initView();
    }

    @Override
    protected void initView() {

    }


    private void pay()
    {
        Map<String, String> map = new HashMap<>();
        map.put("paytype", payMethod == 1 ? "alipay" : "wx");
        map.put("order_id", order_id+"");
        map.put("order_sn", order_sn);
        map.put("money_amount", money);//原价
        map.put("money_amount_cal", money_cal);//折扣之后的价格
        map.put("response_id", "");
        map.put("subject", "一键帮提问");

        PayFeeUtils payFeeUtils = PayFeeUtils.init(this);
        if (payMethod == 1)
        {
            payFeeUtils.alipay(map);
            payFeeUtils.addResulrListener(new PayFeeUtils.PayResulrListener() {
                @Override
                public void onResultSuccess() {
                    finish();
                }

                @Override
                public void onResulrFail() {
                    finish();
                }
            });
        }
        else if (payMethod == 2)
        {
            payFeeUtils.wxPay(map);
        }
    }

    @OnClick({R.id.ll_zfb_pay, R.id.ll_wx_pay, R.id.tv_cancel, R.id.tv_pay_fee})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_wx_pay:
                ivWxPay.setImageResource(R.mipmap.login_f_pressed);
                ivZfbPay.setImageResource(R.mipmap.login_f_normal);
                payMethod = 2;
                break;
            case R.id.ll_zfb_pay:
                ivWxPay.setImageResource(R.mipmap.login_f_normal);
                ivZfbPay.setImageResource(R.mipmap.login_f_pressed);
                payMethod = 1;
                break;
            case R.id.tv_cancel:
                finish();
                break;
            case R.id.tv_pay_fee:
                pay();
                break;
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(MessageEvent messageEvent) {
        if ("payfinish".equals(messageEvent.getMessage())) {
            finish();
        }
    }
}
